﻿var tdBrand=0,tdCar=0,tdStyle=0,tdType=0,tdYear=0,k=0,tdFormat=0,did=0,gid=0;
var act;
var gCurPage = 1;
var gTotalPage = 0;
var gQueryUrl = "";
var gRid = 0;
var gSubPage = 1;
function initSelect(obj,id)
{
	//alert(obj);
	$.get("?act=upload&op=ajaxType&id="+id,function(data){
		var json = eval("("+data+")");	
		if(json.length>0)
		{
			var selop = "<td><a data='"+obj+"' onclick='"+obj+"=0;query(this);'>ALL</a></td><td>";
			for(i=0;i<json.length;i++)
			{
				if(id==1 || id==2 || id==3|| id==4||id==5 || id==50)
				{
					if(json[i].id!=10)
					{
						selop = selop+"&nbsp;&nbsp;&nbsp;&nbsp;<a id='sel_"+id+"' data='"+obj+"' onclick='"+obj+"="+json[i].id+";query(this);'><img src='images/ico/"+json[i].id+".png'/></a>";
					}
				}
				else
				{
					selop = selop+"&nbsp;&nbsp;&nbsp;&nbsp;<a data='"+obj+"' onclick='"+obj+"="+json[i].id+";query(this);'>"+json[i].name+"</a>";
				}
			}
			selop = selop+"</td>";
			$("#"+obj).html("<table cellspacing='0' cellpadding='0' id='tblsel_"+obj+"'>"+selop+"</table>");
			if(obj=="tdStyle")
			{
				$("#trStyle").show();
			}
		}
		else
		{
			var selop = "<a data='"+obj+"' onclick='"+obj+"=0;query(this);'>ALL</a>";
			$("#"+obj).html(selop);
			if(obj=="tdStyle")
			{
				tdStyle = 0;
				$("#trStyle").hide();
			}
		}
		if($("#"+obj).attr("sel")!="")
		{
			$("#"+obj).val($("#"+obj).attr("sel"));
			$("#"+obj).change();
		}
		if(obj=="tdFormat")
		{
			tdBrand=0;
			tdFormat = 42;
			query(null);
			$("#tblsel_tdFormat").find("a")[1].style.color="#FFC019";
		}
	});
}
function clearQueryParam()
{
	tdBrand=0;
	tdCar=0;
	tdStyle=0;
	tdType=0;
	tdYear=0;
	tdFormat=42;
	did = 0;
	gid = 0;
}
function query(o)
{//alert(byname);
    var jq = $(o);
	var fromGroup = jq.attr("data");//alert(fromGroup);
	$("#groupName").html("");
	if(o!=null)
	{
		 
		if(fromGroup=="tdBrand" || fromGroup=="tdCar")
		{
			$("#tblsel_"+fromGroup).find("img").each(function(){
				$(this).css("border","none");
			});
		}
		else
		{
			$("#tblsel_"+fromGroup).find("a").each(function(){
				$(this).css("color","#000000");
			});
		}

		var img = $(o).find("img")[0];
		if(typeof(img) =="undefined")
		{
			o.style.color="#ffc019";
		}
		else
		{
			if(fromGroup=="tdCar" || fromGroup=="tdBrand")
			{
				 
				$(o).find("img").css("border-bottom","1px #ffc019 solid");
			}
		}
	}
	if(tdBrand!=0 && fromGroup=="tdBrand")
	{
		initSelect("tdCar",tdBrand);
	}
	if(tdType!=0  && fromGroup=="tdType")
	{
		initSelect("tdStyle",tdType);
		tdStyle = 0;
	}
	tdFormat = 42;
	gid = 0;
	if(fromGroup=="name")
	{
		var byname = 'name';
		$("#ipostname").attr("style",'');
	}else {
		
		var byname = 'undefined';
		$("#iname").attr("style",'');
	}
	$("#btnBack11").hide();//alert("?act=ajax&op=getLstFileFront&b="+tdBrand+"&c="+tdCar+"&t="+tdType+"&s="+tdStyle+"&y="+tdYear+"&k="+k+"&f="+tdFormat+"&gid="+gid+"&name="+byname);
	getPage("?act=ajax&op=getLstFileFront&b="+tdBrand+"&c="+tdCar+"&t="+tdType+"&s="+tdStyle+"&y="+tdYear+"&k="+k+"&f="+tdFormat+"&gid="+gid+"&name="+byname);
	 
}
function showGroupFile(id,name,type)
{
	$("#btnBack11").show();
	$("#dtid").hide();
	if(type==undefined)
	{
		$("#finame").css('color','');
		$("#fipostname").css('color','');
	}
	
	gCurPage = 1;
	gid = id;
	$("#groupName").html(name);
	$("#groupName").show();
	$("#tid").attr("gid",id);
	$("#tid").attr("fname",name);
	$("#ptid").show();
	if(type=='name') {
		
		var byname = 'name';
	}else {
		var byname = 'undefined';
		
	}
	getPage("?act=ajax&op=getLstFileFront&b="+tdBrand+"&c="+tdCar+"&t="+tdType+"&s="+tdStyle+"&y="+tdYear+"&k="+k+"&f=0&gid="+gid+"&name="+byname);
}
function backShow()
{
	$("#groupName").hide();
	$("#btnBack11").hide();
	$("#ptid").hide();
	$("#dtid").show();
	$("#ipostname").attr("style",'');
	$("#iname").attr("style",'');
	gid=0;
	gCurPage = gSubPage;
	$("#gCurPage").val(gCurPage);
	getPage("?act=ajax&op=getLstFileFront&b="+tdBrand+"&c="+tdCar+"&t="+tdType+"&s="+tdStyle+"&y="+tdYear+"&k="+k+"&f=42"+"&page="+gSubPage);
}
function getPage(url)
{
 
		$("#loading").html("loading.....");
		$("#loading").show();
		$("#ulList").html("");
		$.get(url,function(data){
		var j = eval("("+data+")");
		if(j.st=="ok")
		{
			if(j.data.total>0)
			{
				$("#loading").hide();
				//var ul = "";
				var tbl = "<table width='100%' height='95%' cellpadding='0' cellspacing='0'>"+$("#tblItem").html()+"</table>";
				var tbl2 = "<table width='100%' height='95%' cellpadding='0' cellspacing='0'>"+$("#tblItem2").html()+"</table>";
				for(i=0;i<j.data.data.length;i++)
				{
					var ntbl = "";
					if(j.data.data[i].s==0)
					{
						ntbl = tbl.replace(/SRC/g,j.data.data[i].pic);
						ntbl = ntbl.replace(/NAME/g,j.data.data[i].name);
					}
					else
					{
						ntbl = tbl2.replace(/SRC/g,j.data.data[i].pic);
						ntbl = ntbl.replace(/MARK/g,j.data.data[i].mark);
					    ntbl = ntbl.replace(/NAME/g,j.data.data[i].mark+"（"+j.data.data[i].isGroupTop+"）");
						ntbl = ntbl.replace(/PDATE/g,j.data.data[i].addTime);
					}
					
					ntbl = ntbl.replace(/ID/g,j.data.data[i].id);
					//ntbl = ntbl.replace(/FOR/g,j.data.data[i].ext);
					//ul = ul+'<li id="lifile'+j.data.data[i].id+'">'+ntbl+'</li>';
					$("#ulList").append('<li id="lifile'+j.data.data[i].id+'">'+ntbl+'</li>');
					if(j.data.data[i].e==1)
					{
						$("#lifile"+j.data.data[i].id).find("img").each(function(idx){
							if(j.data.data[i].s!=1 && idx==0)
							{
								
							}
							else
							{
								$(this).show();
							}
							
						});
					}
				}
 
			}
			else
			{
				$("#loading").html("找不到资源");
			}
			gQueryUrl = j.u;
			gTotalPage = j.tp;
			$("#tdTotalPage").html(j.tp);
			$("#total").html(j.data.total);
			$("#txtPageNum").val(gCurPage);
			//$("#divPageNav").html(j.data.pageNav);
			$("#totalNum").html("共有&nbsp;&nbsp;&nbsp;"+j.data.total+"&nbsp;&nbsp;&nbsp;张内容");
		}
		});
}
$(function() {
	    gRid = $("#rid").val();
	    k = getQueryString("k");
		if(k==null)
		{
			k = 0;
		}
		if(k=="newPic")
		{
			setHeaderPos("最新图片");
		}
        else if(k!="0")
		{
			setHeaderPos(decodeURI(k));
		}
		t = getQueryString("t");
		if(t==null)
		{
			t = 0;
		}
		else
		{
			setHeaderPos(decodeURIComponent(getQueryString("tag")));
		}
		act = $("#act").val();
		//query();
	    
		var selop = "<table id='tblsel_tdYear' cellspacing='0' cellpadding='0'><tr><td><a data='tdYear' onclick='tdYear=0;query(this);'>ALL</a></td><td>";
	    for(i=2013;i<=new Date().getFullYear();i++)
		{
			selop = selop+"&nbsp;&nbsp;&nbsp;&nbsp;<a data='tdYear' onclick='tdYear="+i+";query(this);'>"+i+"</a>";	
		}
		$("#tdYear").html(selop+"</td><tr></table>");
		$("#tdCar").html("<a data='tdCar' onclick='tdCar=0;query(this);'>ALL</a>");
	    initSelect("tdBrand",$("#tdBrand").attr("did"));
		initSelect("tdType",$("#tdType").attr("did"));
		initSelect("tdFormat",$("#tdFormat").attr("did"));
		
		$("#tdStyle").html("<a data='tdStyle' onclick='tdStyle=0;query(this);'>ALL</a>");
		//initSelect("tdStyle",$("#tdStyle").attr("did"));
		$("#ulRef").find("li").each(function(){
		    var jq = $(this);
		    jq.click(function(){
		         var t = jq.find("div").html();
		         setHeaderPos(t);
		         getPage("?act=ajax&op=getLstFileFront&k="+encodeURIComponent(t));
		    });
		});
});
function toPage(idx)
{

	if(idx==-1)
	{
		gCurPage = gCurPage -1;
		if(gCurPage<1)
		{	
			gCurPage = 1;
			alert("已到第一页");
			return;
		}
	}
	else if(idx==1)
	{
		gCurPage = gCurPage +1;
		if(gCurPage>gTotalPage)
		{
			alert("已到末尾页");
			gCurPage = gCurPage-1;
			return;
		}
	}
    if(gid==0)
	{
		gSubPage = gCurPage;
		 
	}
	getPage(gQueryUrl+"&page="+gCurPage);
	 
}
function showSet()
{
	$("#querySet").toggle();
}
function doEdt(id,isBao)
{
	if(isBao==0)
	{
		$("#formOpen").attr("action","index.php?act=upload&showEdit="+id);
	}
	else
	{
		$("#formOpen").attr("action","index.php?act=filegroup&showEdit="+id);
	}
	$("#formOpen")[0].submit();
}
function doDel(id,isSub)
{
	if(confirm("确定要下架吗？"))
	{
		 //alert(isSub);
		 $.post("?act=upload&op=unpost&src=ajax",{id:id,isPost:"-1",isSub:isSub},function(d){
		 var j = eval("("+d+")");
		 if(j.st=="ok")
		 {
			 $("#lifile"+id).hide();
		 }
	 	});
	}
}
function by_nameorder(id,name,type)
{	var type = 'name';
	var id = $("#tid").attr("gid");
	var name = $("#tid").attr("fname");
	
	$("#finame").css('color','#ffc019');
	$("#fipostname").css('color','');
	showGroupFile(id,name,type);
}
function by_timeorder(id,name,type)
{	var type = 'undefine';
	var id = $("#tid").attr("gid");
	var name = $("#tid").attr("fname");
	$("#finame").css('color','');
	$("#fipostname").css('color','#ffc019');
	showGroupFile(id,name,type);
}